package com.xuexiang.yoyo.sql.mapper;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import androidx.annotation.NonNull;

import com.xuexiang.yoyo.constant.HesAppStatic;
import com.xuexiang.yoyo.entity.DataReadingTemplateType;

import java.util.ArrayList;
import java.util.List;

public class DataReadingTemplateTypeUserMapper {
    public static final String TABLE = "data_reading_template_type_use";
    private String supplier;
    private String category;
    private String type;
    public static final String CREATE_TABLE_SQL = "CREATE TABLE " + TABLE + "(id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "username TEXT," +
            "category TEXT," +
            "type TEXT," +
            "template TEXT," +
            "template_id INTEGER" +
            ");";


    public static void insert(DataReadingTemplateType item) {
        //删除这个插入 确保唯一
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE,"username=?",new String[]{item.getUsername()});
        ContentValues contentValues = new ContentValues();
        setContentValues(contentValues, item);
        HesAppStatic.getmDataBaseOpenHelper().insert(TABLE, contentValues);
    }

    public static void clear() {
        HesAppStatic.getmDataBaseOpenHelper().delete(TABLE, null, null);
    }

    public static List<DataReadingTemplateType> queryAll() {
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "");
        List<DataReadingTemplateType> resultList = getDeviceTitleUsers(cursor);
        cursor.close();
        return resultList;
    }

    public static List<DataReadingTemplateType> queryByUsername() {
        Cursor cursor = HesAppStatic.getmDataBaseOpenHelper().query(TABLE, "where username='" + HesAppStatic.getUsername() + "'");
        List<DataReadingTemplateType> resultList = getDeviceTitleUsers(cursor);
        cursor.close();
        return resultList;
    }

    private static void setContentValues(ContentValues contentValues, DataReadingTemplateType item) {
        contentValues.put("username", item.getUsername());
        contentValues.put("category", item.getCategory());
        contentValues.put("type", item.getType());
        contentValues.put("template", item.getTemplate());
        contentValues.put("template_id", item.getId());
    }


    public static void batchInsert(List<DataReadingTemplateType> list) {
        //插入之前先清空数据
        clear();
        SQLiteDatabase db = HesAppStatic.getmDataBaseOpenHelper().getWritableDatabase();
        db.beginTransaction();
        try {
            ContentValues contentValues = new ContentValues();
            for (DataReadingTemplateType item : list) {
                setContentValues(contentValues, item);
                db.insert(TABLE, null, contentValues);
                contentValues.clear();
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();

        }
    }


    @NonNull
    private static List<DataReadingTemplateType> getDeviceTitleUsers(Cursor cursor) {
        List<DataReadingTemplateType> resultList = new ArrayList<>();
        while (cursor.moveToNext()) {
            DataReadingTemplateType item = new DataReadingTemplateType();
            item.setId(cursor.getInt(0));
            item.setUsername(cursor.getString(1));
            item.setCategory(cursor.getString(2));
            item.setType(cursor.getString(3));
            item.setTemplate(cursor.getString(4));
            item.setTemplateId(cursor.getLong(5));
            resultList.add(item);
        }
        return resultList;
    }


}
